package com.itheima.service.impl;

import com.itheima.common.dto.LoginDto;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void login(LoginDto loginDto) {

        String code = (String) redisTemplate.opsForValue().get(loginDto.getTelephone());

        if (!code.equals(loginDto.getValidateCode())) {
            throw new RuntimeException("验证码错误");
        }

        Member member = memberMapper.findByTelephone(loginDto.getTelephone());

        if (member == null) {
            //该用户不是会员需要创建会员
            member = new Member();
            member.setPhoneNumber(loginDto.getTelephone());
            member.setRegTime(new Date());
            memberMapper.insert(member);
        }

    }
}
